home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Examples / CExamples / EditCdev.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-03  |  5.2 KB  |  184 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    Macintosh Developer Technical Support
  4. #
  5. #    EditText Sample Control Panel Device
  6. #
  7. #    EditCdev
  8. #
  9. #    EditCdev.c    -    C Source
  10. #
  11. #    Copyright © 1988, 1995 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.0                    8/88
  15. #
  16. #    Components:    EditCdev.c            August 1, 1988
  17. #                EditCdev.r            August 1, 1988
  18. #                EditCdev.make        August 1, 1988
  19. #
  20. #    EditCdev is a sample Control Panel device (cdev) that 
  21. #    demonstrates the usage of the edit-related messages.  
  22. #    EditCdev demonstrates how to implement an editText item
  23. #    in a Control Panel Device.  It utilizes the new undo, cut, copy,
  24. #    paste, and delete messages that are sent to cdevs in
  25. #    response to user menu selections.
  26. #
  27. #    It is comprised of two editText items that can be edited 
  28. #    and moved between via the mouse or tab key.
  29. #
  30. ------------------------------------------------------------------------------*/
  31.  
  32. #include <Types.h>
  33. #include <Memory.h>
  34. #include <Quickdraw.h>
  35. #include <TextEdit.h>
  36. #include <Dialogs.h>
  37. #include <Events.h>
  38. #include <Devices.h>
  39. #include <Scrap.h>
  40.  
  41. /* Constants */
  42. #define    textItm        1            /* first editTExt item in cdev */
  43.  
  44. #define    undoDev        9            /* cdev edit messages */
  45. #define    cutDev        10
  46. #define    copyDev        11
  47. #define    pasteDev    12
  48. #define    clearDev    13
  49.  
  50.  
  51. /* Types */
  52. typedef struct CDEVRec {
  53.     TEHandle    myTE;
  54. } CDEVRec, *CDEVPtr, **CDEVHnd;
  55.     
  56. /* Prototypes */
  57. void DoEditCommand (short message, DialogPtr CPDialog);
  58. pascal Handle
  59. TextCDEV(short message, short item, short numItems, short CPanelID,
  60.          EventRecord *theEvent, Handle cdevStorage, DialogPtr CPDialog);
  61.  
  62. /* This is the main dispatcher. It must be the first code in the cdev.
  63.  EditCdev's dispatcher responds only to the following messages from
  64.  the Control Panel:
  65.      
  66.     macDev        - To indicate what machines it is available on.
  67.     initDev        - To set up some temporary storage and get the caret started.
  68.     keyEvtDev    - To check for an edit command and do the appropriate action.
  69.     cutDev        - To cut the current selection.
  70.     copyDev        - To copy the current selection.
  71.     pasteDev    - To paste the contents of the clipboard.
  72.     clearDev    - To delete the current selection.
  73.  
  74.  The Dialog Manager's services are used to handle entry of text, selection
  75.  of text, editing of text, and moving between the editText items via the
  76.  tab key. Since the Dialog Manager handles the selection of text, we do not
  77.  have to be concerned with hitDev messages for the editText items. The only
  78.  things we have to take care of are calling the Dialog Manager editing
  79.  routines in response to an edit command, and getting the caret to show up
  80.  at the beginning. In response to an edit command that was the result of
  81.  a command-key equivalent, we must also eliminate the event so that it does
  82.  not get processed as a keyDown by the Dialog Manager. Otherwise, an 'x'
  83.  would show up in the editText item when the user did a command-x to cut
  84.  the text.*/
  85.  
  86. pascal Handle
  87. TextCDEV(
  88.     short        message,
  89.     short        item,
  90.     short        numItems,
  91.     short        CPanelID,
  92.     EventRecord    *theEvent,
  93.     Handle        cdevStorage,
  94.     DialogPtr    CPDialog
  95. )
  96. {
  97.     #pragma unused (item, CPanelID)        /* unused formal parameters */
  98.     
  99.     char        tempChar;
  100.  
  101.     if (message == macDev) return((Handle) 1);                /* we work on every machine */
  102.     else if (cdevStorage != nil) {
  103.         switch (message) {
  104.             case initDev:                                    /* initialize cdev */
  105.                 cdevStorage = NewHandle(sizeof(CDEVRec));    /* create provate storage */
  106.                 SelectDialogItemText(CPDialog, numItems + textItm, 0, 999); /* make caret show up */
  107.                 break;
  108.                 
  109.             case hitDev:                                    /* handle hit on item */
  110.             case closeDev:                                    /* clean up and dispose */
  111.             case nulDev:
  112.             case updateDev:                                    /* handle any update drawing */
  113.             case activDev:                                    /* activate any needed items */
  114.             case deactivDev:                                /* deactivate any needed items */
  115.                 break;
  116.                 
  117.             case keyEvtDev:                                    /* respond to keydown */
  118.                 tempChar = theEvent->message & charCodeMask;/* get the character, and check */
  119.                 if (theEvent->modifiers & cmdKey) {            /*  status of command key */
  120.                     message = nulDev;                        /* start with no message */
  121.                     theEvent->what = nullEvent;                /* and empty event type */
  122.                     
  123.                     switch (tempChar) {                        /* set appropriate message */
  124.                         
  125.                         case 'X':
  126.                         case 'x':
  127.                             message = cutDev;
  128.                             break;
  129.                         case 'C':
  130.                         case 'c':
  131.                             message = copyDev;
  132.                             break;
  133.                         case 'V':
  134.                         case 'v':
  135.                             message = pasteDev;
  136.                             break;
  137.                     }
  138.                     DoEditCommand(message, CPDialog);        /* Let edit command handler take it */
  139.                 }
  140.                 break;
  141.                 
  142.             case macDev:
  143.             case undoDev:
  144.                 break;
  145.                 
  146.             case cutDev:
  147.             case copyDev:
  148.             case pasteDev:
  149.             case clearDev:
  150.                 DoEditCommand(message, CPDialog);            /* respond to edit command */
  151.                 break;
  152.         }
  153.  
  154.         return (cdevStorage);
  155.     }  /* cdevStorage != nil */
  156.     
  157.     /* 
  158.     **    if cdevStorage = NIL then ControlPanel 
  159.     **  will put up memory error
  160.     */
  161.     return (nil);
  162. }
  163.  
  164. /* Call the appropriate Dialog Manager routine to handle an edit command for
  165.      an editText item. It will do all the work regarding the TEScrap. */
  166. void DoEditCommand (short message, DialogPtr CPDialog)
  167. {
  168.     switch (message) {
  169.     case cutDev:
  170.         DialogCut(CPDialog);
  171.         break;
  172.     case copyDev:
  173.         DialogCopy(CPDialog);
  174.         break;
  175.     case pasteDev:
  176.         DialogPaste(CPDialog);
  177.         break;
  178.     case clearDev:
  179.         DialogDelete(CPDialog);
  180.         break;
  181.     }
  182. }
  183.  
  184.